package patrol.mobile.com.mobilepatrol.fragment;

import android.annotation.SuppressLint;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.AppConfig;
import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.road_survey.TrafficInvestDetailActivity2;
import patrol.mobile.com.mobilepatrol.adapter.RoadEditAdapter;
import patrol.mobile.com.mobilepatrol.bean.RouteSurveyTaskDataBO;
import patrol.mobile.com.mobilepatrol.bean.RouteSurveyTaskStructBO;
import patrol.mobile.com.mobilepatrol.bean.TrafficInvestBaseInfo;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.UIHelper;

/**
 * Created by apple on 18/1/22.
 */
@SuppressLint({"NewApi", "ValidFragment"})
public class BaseRoadFragment extends BaseFragment {
    RouteSurveyTaskStructBO structID;
    public TrafficInvestBaseInfo isReview;
    //
    @BindView(R.id.bt_next)
    public Button bt_next;
    //
    @BindView(R.id.bt_last)
    public Button bt_last;
    //
    @Nullable
    @BindView(R.id.bt_cur)
    public Button bt_cur;
    //
    @BindView(R.id.recycler)
    public ListView lv;

    public int pos;
    public RoadEditAdapter mAdapter;
    public DBHelper dbHelper;

    List<Map<String, String>> saveDatas = new ArrayList<Map<String, String>>();
    public List<RouteSurveyTaskDataBO> dataBOs;
    public String type;

    @Override
    public int getLayoutId() {
        return R.layout.list_edit;
    }

    @Override
    public void initView() {
        super.initView();

        setCur(pos);
    }

    public BaseRoadFragment() {
    }

    public BaseRoadFragment(int pos, RouteSurveyTaskStructBO structID, TrafficInvestBaseInfo isReview, String type) {
        this.structID = structID;
        this.isReview = isReview;
        this.pos = pos;
        this.type = type;
    }

    @Override
    public void initData() {
        dbHelper = new DBHelper(getActivity());
//        dataBOs= dbHelper.getTaskDataInfo(structID.getiD(), "沥青路面");
//        mAdapter = new RoadEditAdapter(getActivity(),dataBOs,pos);
//        lv.setAdapter(mAdapter);
        updateData(type);
    }

    @Override
    public void initListener() {
        bt_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int page = structID.counts.size();
                if (pos == structID.counts.get(page - 1)) {
                    showToast("最后一页");
                } else {
                    pos = pos + 1;
//                    dataBOs= dbHelper.getTaskDataInfo(structID.getiD(), "沥青路面");
//                    mAdapter = new RoadEditAdapter(getActivity(),dataBOs,pos);
//                    lv.setAdapter(mAdapter);
                    updateData(type);
                }
                setCur(pos);

            }
        });
        bt_last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos == structID.counts.get(0)) {
                    showToast("最前一页");
                } else {
                    pos = pos - 1;
//                    dataBOs= dbHelper.getTaskDataInfo(structID.getiD(), "沥青路面");
//                    mAdapter = new RoadEditAdapter(getActivity(),dataBOs,pos);
//                    lv.setAdapter(mAdapter);
                    updateData(type);
                }
                setCur(pos);
            }
        });
    }

    public void setCur(int pos) {
        if (bt_cur != null) {
            switch (pos) {
                case 0:
                    bt_cur.setText("当前\n0-100");
                    break;
                case 1:
                    bt_cur.setText("当前\n100-200");
                    break;
                case 2:
                    bt_cur.setText("当前\n200-300");
                    break;
                case 3:
                    bt_cur.setText("当前\n300-400");
                    break;
                case 4:
                    bt_cur.setText("当前\n400-500");
                    break;
                case 5:
                    bt_cur.setText("当前\n500-600");
                    break;
                case 6:
                    bt_cur.setText("当前\n600-700");
                    break;
                case 7:
                    bt_cur.setText("当前\n700-800");
                    break;
                case 8:
                    bt_cur.setText("当前\n800-900");
                    break;
                case 9:
                    bt_cur.setText("当前\n900-1000");
                    break;

            }
        }

    }

    public List<Map<String, String>> getSaveData() {
        List<RouteSurveyTaskDataBO> data = mAdapter.save();
        saveDatas.clear();
        for (int i = 0; i < data.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("id", data.get(i).getiD());
            if (isReview.surveyType.equals("调查")) {
                switch (pos) {
                    case 0:
                        map.put("surveyOne", data.get(i).surveyOne + "");
                        break;
                    case 1:
                        map.put("surveyTwo", data.get(i).surveyTwo + "");
                        break;
                    case 2:
                        map.put("surveyThree", data.get(i).surveyThree + "");
                        break;
                    case 3:
                        map.put("surveyFour", data.get(i).surveyFour + "");
                        break;
                    case 4:
                        map.put("surveyFive", data.get(i).surveyFive + "");
                        break;
                    case 5:
                        map.put("surveySix", data.get(i).surveySix + "");
                        break;
                    case 6:
                        map.put("surveySeven", data.get(i).surveySeven + "");
                        break;
                    case 7:
                        map.put("surveyEight", data.get(i).surveyEight + "");
                        break;
                    case 8:
                        map.put("surveyNine", data.get(i).surveyNine + "");
                        break;
                    case 9:
                        map.put("surveyTen", data.get(i).surveyTen + "");
                        break;

                }
            } else {
                switch (pos) {
                    case 0:
                        map.put("checkOne", data.get(i).checkOne + "");
                        break;
                    case 1:
                        map.put("checkTwo", data.get(i).checkTwo + "");
                        break;
                    case 2:
                        map.put("checkThree", data.get(i).checkThree + "");
                        break;
                    case 3:
                        map.put("checkFour", data.get(i).checkFour + "");
                        break;
                    case 4:
                        map.put("checkFive", data.get(i).checkFive + "");
                        break;
                    case 5:
                        map.put("checkSix", data.get(i).checkSix + "");
                        break;
                    case 6:
                        map.put("checkSeven", data.get(i).checkSeven + "");
                        break;
                    case 7:
                        map.put("checkEight", data.get(i).checkEight + "");
                        break;
                    case 8:
                        map.put("checkNine", data.get(i).checkNine + "");
                        break;
                    case 9:
                        map.put("checkTen", data.get(i).checkTen + "");
                        break;

                }
            }


            saveDatas.add(map);
        }
        return saveDatas;
    }

    public void saveData() {
        CommonUtil.hiddenKeyBoard(getActivity());
        TrafficInvestDetailActivity2 activity2 = (TrafficInvestDetailActivity2) getActivity();
        synchronized (dbHelper) {
            isReview.setStatus(0);
            dbHelper.updateInvestBaseInfo(isReview);
            dbHelper.updateRouteSurveyTaskStructInfo(structID, activity2.getCurrentRoadWidth(), pos);
            dbHelper.updateRouteSurveyUpdateDataInfo(getSaveData());
        }
        //上次记录
        String lastInvestPage = structID.getiD() + "-" + type + "-" + pos;
        Log.d("shj--", lastInvestPage);
        AppConfig.getAppConfig(getActivity()).set(AppConfig.CONF_INVEST_LAST_PAGE, lastInvestPage);
        UIHelper.ToastMessageCenter(getActivity(), "保存成功");
    }

    public void updateData(String type) {
        dataBOs = dbHelper.getTaskDataInfo(structID.getiD(), type);
        mAdapter = new RoadEditAdapter(getActivity(), dataBOs, pos, isReview.surveyType);
        lv.setAdapter(mAdapter);
    }


}
